
package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;


import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import apps.softtech.Utility.ApplicationConstants;
import apps.softtech.Utility.RequestDataFromServer;
import apps.softtech.fastmarket.R;
import apps.softtech.fastmarket.model.DatabaseHandler;
import apps.softtech.fastmarket.model.FollowUser;
import apps.softtech.fastmarket.model.UserInfo;

public class AdapterMyProfileFollower extends BaseAdapter{

	ArrayList<UserInfo> mFollowUsersList = new ArrayList<UserInfo>();
	ArrayList<FollowUser> mFollowList = new ArrayList<FollowUser>();
	LayoutInflater inf;
	Context mContext;
	DisplayImageOptions options;
	DatabaseHandler dbh;
	int myId=2;
	public AdapterMyProfileFollower(Context mContext,ArrayList<UserInfo> mFollowUsersList,DisplayImageOptions options 
			){
		this.mContext = mContext;
		this.mFollowUsersList = mFollowUsersList;
		inf = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.options=options;
		dbh = new DatabaseHandler(mContext);
//		/
	}
	@Override
	public int getCount() {
		return mFollowUsersList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View vi, ViewGroup arg2) {
		if(mFollowUsersList.size()==0||mFollowUsersList==null){
			vi = inf.inflate(R.layout.search_no_result, null);
			TextView tvNoResult = (TextView)vi.findViewById(R.id.tvNoResult);
			tvNoResult.setText("No follower");
			return vi;
		}
		if(vi==null){
			vi=inf.inflate(R.layout.item_my_profile_follower, null);
			vh = new ViewHolder();
			vh.ivFollowing = (ImageView)vi.findViewById(R.id.ivMyProfileFollower);
			vh.tvFollowing = (TextView)vi.findViewById(R.id.tvMyProfileFollowerInfo);
			vh.btUnfollow = (Button)vi.findViewById(R.id.btSetFollowing);
			vi.setTag(vh);
		}else{
			vh = (ViewHolder) vi.getTag();
		}
		vh.tvFollowing.setText(""+mFollowUsersList.get(position).getUserName()+
				mFollowUsersList.get(position).getSt_UserId());
		String url=ApplicationConstants.BASE_URL_IMAGE + mFollowUsersList.get(position).getAvatarPath();
		ImageLoader.getInstance().displayImage(url, vh.ivFollowing, options);
		//
		//get id to unfollow
		//
		
		final int id = position;
		vh.btUnfollow.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				ToId = mFollowUsersList.get(id).getSt_UserId();
				new AsyncTaskUnfollow(id).execute();
			}
		});
		return vi;
	}
	/**
	 * id remove
	 */
	
	
	/**
	 * to id
	 */
	int ToId;
	ViewHolder vh = null;
	class ViewHolder{
		ImageView ivFollowing;
		TextView tvFollowing;
		Button btUnfollow;
	
	}
	class AsyncTaskUnfollow extends AsyncTask<Void, Void, Void>{
		ProgressDialog pd;
		int success;
		int postion;
		public AsyncTaskUnfollow(int id) {
			postion=id;
		}
		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(mContext);
			pd.setMessage("Unfollow....");
			pd.show();//
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... params) {
			
			try {
				JSONObject json = new JSONObject(new RequestDataFromServer().requestUnfollow(myId, ToId));
				 success = json.getInt("success");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			if(success==1){
				mFollowUsersList.remove(postion);
				notifyDataSetChanged();
			}else{
				Toast.makeText(mContext, "not Unfollow!", Toast.LENGTH_LONG).show();
			}
			pd.dismiss();
			super.onPostExecute(result);
		}
	}
}

